package com.ayz.gateway.core.core;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @ClassName BaseHandler
 * @Description TODO: Netty Channel的处理器
 * @Date 2023/5/5 17:23
 * @Version 1.0
 **/

/**
 *
 * 记住用户每一次请求访问网关，都定义成一次会话
 * 一次Http会话包括
 * 1. 连接建立
 * 2. Http请求包解码
 * 3. 协议转换(Http->RPC)
 * 4. 根据请求路径映射到具体的RPC方法
 * 5. 发起RPC调用
 * 6. 封装HTTP响应并返回
 * @param <T>
 */
@EqualsAndHashCode(callSuper = true)
@Data
public abstract class BaseHandler<T> extends SimpleChannelInboundHandler<T> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, T request) throws Exception {
        this.openSession(channelHandlerContext,channelHandlerContext.channel(),request);
    }

    public abstract void openSession(ChannelHandlerContext channelHandlerContext, final Channel channel, T request);
}
